package cn.edu.cdu.farmdeal.service.impl;

import cn.edu.cdu.farmdeal.domain.*;
import cn.edu.cdu.farmdeal.mapper.*;
import cn.edu.cdu.farmdeal.service.DetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.SHA;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: tupeng
 * @Date: 2020/3/30
 **/
@Service
public class DetailServiceImpl implements DetailService {
    @Autowired
    private ImageMapper imageMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopEntityMapper shopEntityMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private CartShopProMapper cartShopProMapper;
    @Autowired
    private TempProMapper tempProMapper;

    @Override
    public int saveTempPro(Map<String, Object> map) {
        String productId = (String) map.get("productId");
        String productImg = (String) map.get("productImg");
        String productName = (String) map.get("productName");
        String name = (String) map.get("name");
        BigDecimal price = (BigDecimal) map.get("price");
        int buySum = (int) map.get("buySum");
        String specification = (String) map.get("specification");
        int shopId = (int) map.get("shopId");
        String userName = (String) map.get("userid");
        BigDecimal sumPrice = price.multiply(BigDecimal.valueOf(buySum));
        //插入临时表
        TempPro tempPro = new TempPro();
        tempPro.setBuySum(buySum);
        tempPro.setProductName(productName);
        tempPro.setPrice(price);
        tempPro.setSumPrice(sumPrice);
        tempPro.setUserId(userName);
        tempPro.setShopId(shopId);
        tempProMapper.insertSelective(tempPro);
        return 1;
    }


    @Override
    public int commitCart(Map<String, Object> map) {
        String productId = (String) map.get("productId");
        String productImg = (String) map.get("productImg");
        String productName = (String) map.get("productName");
        String name = (String) map.get("name");
        String price = (String) map.get("price");
        String buySum = (String) map.get("buySum");
        String specification = (String) map.get("specification");
        String shopId = (String) map.get("shopId");
        String userName = (String) map.get("userid");
        BigDecimal sumPrice = new BigDecimal(price).multiply(BigDecimal.valueOf(Integer.parseInt(buySum)));
        //获得商店信息
        ShopEntityExample shopEntityExample = new ShopEntityExample();
        shopEntityExample.createCriteria().andIdEqualTo(Integer.parseInt(shopId));
        List<ShopEntity> entityList = shopEntityMapper.selectByExample(shopEntityExample);
        ShopEntity shopEntity = entityList.get(0);
        String shopName = shopEntity.getShopName();
        //获得cart信息
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andUserIdEqualTo(userName);
        List<Cart> cartList = cartMapper.selectByExample(cartExample);
        //获得Cart—shop信息
//        ShopExample shopExample = new ShopExample();
//        shopExample.createCriteria().andIdEqualTo(Integer.parseInt(shopId));

        //插入cart
        Cart cart = new Cart();
        cart.setUserId(userName);
        if (cartList == null || cartList.size() == 0) {
            cartMapper.insertSelective(cart);
        }
        //获取插入的cartid
        Cart cart1 = cartMapper.getId(userName);

        //插入shop
        Shop shop = new Shop();
        shop.setShopName(shopName);
        shop.setUserName(name);
        shop.setCartId(cart1.getId());
        shop.setId(Integer.parseInt(shopId));
        //userid作为cartid
//        shop.setUserId(String.valueOf(cart1.getId()));
        if (cart1 != null) {
            List<Shop> shopList = shopMapper.selectShop(cart1.getId());
            if (shopList == null || shopList.size() == 0) {
                shopMapper.insertShop(shop);
            } else {
                List<Shop> shopList1 = shopMapper.selectByName(shopName);
                if (shopList1 == null || shopList1.size() == 0) {
                    shopMapper.insertShop(shop);
                }
            }
        }
        //插入产品
        CartShopPro cartShopPro = new CartShopPro();
        cartShopPro.setProductId(Integer.parseInt(productId));
        cartShopPro.setProductImg(productImg);
        cartShopPro.setProductName(productName);
        cartShopPro.setName(name);
        cartShopPro.setPrice(new BigDecimal(price));
        cartShopPro.setBuySum(Integer.parseInt(buySum));
        cartShopPro.setSpecification(specification);
        cartShopPro.setShopId(Integer.parseInt(shopId));
        cartShopPro.setUserId(userName);
        cartShopPro.setSumPrice(sumPrice);
        cartShopProMapper.insertSelective(cartShopPro);
        return 1;
    }


    @Override
    public Shop checkPro(String id, String userName) {
        return shopMapper.checkPro(id, userName);
    }

    @Override
    public List<Image> getImage(int id) {
        ImageExample imageExample = new ImageExample();
        imageExample.createCriteria().andProductIdEqualTo(id);
        return imageMapper.selectByExample(imageExample);
    }

    @Override
    public Product getDetail(int id) {
        Product product = productMapper.getDetail(id);
        Date updateDate = product.getUpdateTime();
        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        String dateString = dateFormat.format(updateDate);
        product.setUpdateTimeString(dateString);
        int shopId = product.getShopId();
        ShopEntityExample shopEntityExample = new ShopEntityExample();
        shopEntityExample.createCriteria().andIdEqualTo(shopId);
        List<ShopEntity> shopEntities = shopEntityMapper.selectByExample(shopEntityExample);
        product.setUserName(shopEntities.get(0).getUserName());
        return product;
    }

    @Override
    public List<Product> getShopHot(int id) {
        int shopId = productMapper.getShopId(id);
        return productMapper.getShopHot(shopId);
    }

    @Override
    public List<Specification> getSpec(int id) {
        SpecificationExample specificationExample = new SpecificationExample();
        specificationExample.createCriteria().andProductIdEqualTo(id);
        return specificationMapper.selectByExample(specificationExample);
    }


}
